home *** CD-ROM | disk | FTP | other *** search
/ F1 Licenseware / F1 Licenseware - Volume 1.iso / disks / 089a.dms / 089a.adf / TEXTS / CHAPTERS_21-30 / Chapter29.txt < prev    next >
Text File  |  1992-03-06  |  5KB  |  150 lines

  1.                      The Absolute Beginners Guide To Amos
  2.                     -------------------------------------
  3.                              Chapter Twenty Nine
  4.                              --------------------
  5. Imagine an enormous game like Populous or Elite without a save game option!
  6. They would be almost unplayable without it.
  7.  
  8. I would now like to show you how to set up a load and save game feature
  9. for your own programs.
  10. It's a fairly straight forward affair doing this in Amos but it has NEVER
  11. been covered anywhere, not in the Amos manual, books or magazines that I 
  12. have seen.  So how is a beginner meant to know how to do this?
  13.  
  14. Don't worry, read on.
  15.  
  16. When you save a games position all you have to do is save ALL of the
  17. variables into a file. It's as simple as that!
  18.  
  19. First, decide where you are going to put your saved game feature, the main
  20. menu would be the usual place. Set up your menu with LOAD and SAVE in it 
  21. and when selected by the user call the Procedures _Load and _Save 
  22. respectively.
  23.  
  24. We of course, need to write the _Load and _Save procedures first.
  25. Let's look at writing _Save first.
  26.  
  27. First of all we need to have a list of EVERY variable used in the game,
  28. you may not actually need to save all of them, but if you are not sure about
  29. any particular one then include it anyway. 
  30. Once you have written down all of your programs variables you are ready to
  31. write the _Save game procedure.
  32.  
  33. To keep this example simple we will not offer the user a file selector but
  34. we will save the games position with a predefined name. This means only one 
  35. game can ever be saved on a single disk as any subsequent saves will 
  36. overwrite the last save. This keeps things very simple and will hopefully 
  37. mean you will understand it a lot easier. Of course, once you have learnt
  38. the basics of loading and saving variables you can expand these procedures
  39. into your own custom routines, specific to your program.
  40.  
  41. I am going to make up an imaginary game that has only four variables in it.
  42. The variables are:
  43.  
  44. POINTS
  45. FRUITS (5)
  46. MONSTER$(25)
  47. FIRE$
  48.  
  49. As we now know all of the variables of our game we can PRINT them to a disk!
  50. But first we must OPEN a channel. You do not need to understand how this
  51. works, just remember that when you OPEN a channel it MUST be closed after
  52. you have finished with it.
  53.  
  54. Open Out 1,"DF0:SAVED_GAME"
  55.  
  56. This OPENs a channel which we will call 1 for our reference. You can have
  57. more than one channel open at a time, but I doubt you will ever find the need
  58. to, so just use 1 all of the time. The "DF0:SAVED_GAME" is the path and
  59. file name we will be creating on the disk. Remember the comma and the quotes 
  60. around the file name. You can put in any path or call the file anything you 
  61. wish of course.
  62.  
  63. Now we can get to work on actually saving the variables onto the disk.
  64.  
  65. PRINT #1,POINTS
  66.  
  67. This line will save the variable POINTS and it's current value to the file we
  68. have called SAVED_GAME. The #1 tells Amos we are printing to channel one.
  69.  
  70. Now we want to save the dimensioned array FRUITS() which has five elements.
  71. This too is fairly simple.
  72.  
  73. For A=1 To 5
  74. Print #1,FRUITS(A)
  75. Next A
  76.  
  77. We have used a For Next loop to put the five elements onto disk which is
  78. stored along with POINTS in the file "SAVED_GAME"
  79.  
  80. Next we have MONSTER$() a 25 element string array. This is virtually the
  81. same.
  82.  
  83. For A=1 To 25
  84. Print #1,MONSTER$(A)
  85. Next A
  86.  
  87. And lastly, we have a nice simple string variable.
  88.  
  89. PRINT #1,FIRE$
  90.  
  91. Don't forget to CLOSE channel 1,
  92.  
  93. CLOSE 1
  94.  
  95. And that's it, all the four variables and their values are all stored neatly
  96. into one file on disk ready to restore the game to it's original position. 
  97. So, lumping it all together, here is the Procedure in full.
  98.  
  99. Procedure _SAVE
  100.  
  101. Open Out 1,"DF0:SAVED_GAME"
  102.  
  103. PRINT #1,POINTS
  104.  
  105. For A=1 To 5
  106. Print #1,FRUITS(A)
  107. Next A
  108.  
  109. For A=1 To 25
  110. Print #1,MONSTER$(A)
  111. Next A
  112.  
  113. PRINT #1,FIRE$
  114.  
  115. CLOSE 1
  116. End Proc
  117.  
  118. Obviously to make use of this procedure in your own programs you will need
  119. to enter the variables pertaining to your own particular program.
  120.  
  121. Now we must look at how to load back the variables into your program. 
  122. Luckily it's all very simple if you understood how the save part works. 
  123. All we need to do is substitute the PRINT statements for INPUT statements. 
  124. So all we have to do is cut and paste the _save procedure, rename it _LOAD
  125. and change the PRINTs to INPUTs.
  126.  
  127. Procedure _LOAD
  128. Open In 1,"df0:SAVED_GAME"
  129.  
  130. Input #1,POINTS
  131.  
  132. For A=1 To 5
  133. Input #1,FRUITS(A)
  134. Next A
  135.  
  136. For A=1 To 25
  137. Input #1,MONSTER$(A)
  138. Next A
  139.  
  140. Input #1,FIRE$
  141.  
  142. CLOSE 1
  143. End Proc
  144.  
  145. See example29.Amos for a small demonstration.
  146.  
  147.  
  148.                          End of Chapter Twenty Nine
  149.                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  150.